﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bowerbird.Website.Models;
using Bowerbird.Api.Dtos;

namespace Bowerbird.Website.Controllers
{
    [Authorize]
    public class ProfileController : ControllerBase
    {
        public ActionResult Edit()
        {
            UserModel user = CurrentUser;

            Claims claims = TryReadClaims();

            ProfileEditModel model = new ProfileEditModel
            {
                Id = user.Id,
                Name = user.Name,
                Email = user.Email,
                ProfilePicture = user.ProfilePictureUrl
            };

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(ProfileEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            // ToDo: API Call
            User user = new User()
            {
                id = model.Id,
                name = model.Name,
                email = model.Email,
                profilePicture = model.ProfilePicture
            };

            user = ApiWrapper.EditUserProfile(user);

            CurrentUser = new UserModel
            {
                Id = user.id,
                Name = user.name,
                NameIdentifier = user.nameIdentifier,
                IdentityProvider = user.identityProvider,
                Email = user.email,
                ProfilePictureUrl = user.profilePicture,
                IsAuthenticated = Request.IsAuthenticated
            };

            return RedirectToAction("Index", "Home");
        }
    }
}
